package com.quartet.modules.statistics.Enum;

import com.quartet.modules.statistics.dto.ServiceStatisticsRequestDto;
import com.quartet.modules.statistics.dto.StatisticsRequestDto;
import com.quartet.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 查询周期
 * </p>
 *
 * @author Zheng Jie
 * @date 2020-05-07
 */
@Getter
@AllArgsConstructor
public enum QueryPeriodEnum {


    YESTERDAY("1", "昨天") {
        @Override
        public void setFilterTime(StatisticsRequestDto statisticsRequestDto) {
            statisticsRequestDto.setStartTime(DateUtil.getLastDayMin(1));
            statisticsRequestDto.setEndTime(DateUtil.getLastDayMax(1));
        }

        @Override
        public List<String> getAxis() {
            return getHourAxis();
        }
    },
    TODAY("2", "今天") {
        @Override
        public void setFilterTime(StatisticsRequestDto statisticsRequestDto) {
            statisticsRequestDto.setStartTime(DateUtil.getLastDayMin(0));
            statisticsRequestDto.setEndTime(DateUtil.getLastDayMax(0));
        }

        @Override
        public List<String> getAxis() {
            return getHourAxis();
        }
    },
    LAST_THREE_DAYS("3", "近三天") {
        @Override
        public void setFilterTime(StatisticsRequestDto statisticsRequestDto) {
            statisticsRequestDto.setStartTime(DateUtil.getLastDayMin(2));
            statisticsRequestDto.setEndTime(DateUtil.getLastDayMax(0));
        }

        @Override
        public List<String> getAxis() {
            return getDayAxis(3);
        }
    },
    LAST_SEVEN_DAYS("4", "近七天") {
        @Override
        public void setFilterTime(StatisticsRequestDto statisticsRequestDto) {
            statisticsRequestDto.setStartTime(DateUtil.getLastDayMin(6));
            statisticsRequestDto.setEndTime(DateUtil.getLastDayMax(0));
        }

        @Override
        public List<String> getAxis() {
            return getDayAxis(7);
        }
    },
    LAST_FIFTEEN_DAYS("5", "近十五天") {
        @Override
        public void setFilterTime(StatisticsRequestDto statisticsRequestDto) {
            statisticsRequestDto.setStartTime(DateUtil.getLastDayMin(14));
            statisticsRequestDto.setEndTime(DateUtil.getLastDayMax(0));
        }

        @Override
        public List<String> getAxis() {
            return getDayAxis(15);
        }
    },
    LAST_THIRTY_DAYS("6", "近三十天") {
        @Override
        public void setFilterTime(StatisticsRequestDto statisticsRequestDto) {
            statisticsRequestDto.setStartTime(DateUtil.getLastDayMin(29));
            statisticsRequestDto.setEndTime(DateUtil.getLastDayMax(0));
        }

        @Override
        public List<String> getAxis() {
            return getDayAxis(30);
        }
    };
    private final String value;
    private final String description;

    public static QueryPeriodEnum find(String val) {
        for (QueryPeriodEnum dataScopeEnum : QueryPeriodEnum.values()) {
            if (val.equals(dataScopeEnum.getValue())) {
                return dataScopeEnum;
            }
        }
        return null;
    }

    /**
     * 获取按照小时的周期坐标
     */
    public static List<String> getHourAxis() {
        List<String> axis = new ArrayList<>();
        LocalDateTime localDateTime = LocalDateTime.now();
        for (int i = 23; i >= 0; i--) {
            String s = DateUtil.localDateTimeFormat(localDateTime.minusHours(i), "HH");
            axis.add(s);
        }
        return axis;
    }

    /**
     * 获取按照天的周期坐标
     */
    public static List<String> getDayAxis(int n) {
        List<String> axis = new ArrayList<>();
        LocalDateTime localDateTime = LocalDateTime.now();
        for (int i = n - 1; i >= 0; i--) {
            String s = DateUtil.localDateTimeFormat(localDateTime.minusDays(i), "MM-dd");
            axis.add(s);
        }
        return axis;
    }

    /**
     * 功能描述: 自定义区间获取月横左边
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/11/11 21:09
     * @param startTime
     * @param endTime
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getBetweenMonthAxis(LocalDateTime startTime, LocalDateTime endTime) {
        List<String> axis = new ArrayList<>();
        while (startTime.isBefore(endTime)){
            String timeFormat = DateUtil.localDateTimeFormat(startTime, "yy-MM");
            axis.add(timeFormat);
            startTime=startTime.plusMonths(1);
        }
        axis.add(DateUtil.localDateTimeFormat(startTime,"yy-MM"));
        return axis;
    }

    /**
     * 功能描述: 自定义区间获取天横左边
     * @author huipanxing huipanxing@chinasofti.com
     * @date 2021/11/11 21:09
     * @param startTime
     * @param endTime
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getBetweenDayAxis(LocalDateTime startTime, LocalDateTime endTime) {
        List<String> axis = new ArrayList<>();
        while (startTime.isBefore(endTime)){
            String timeFormat = DateUtil.localDateTimeFormat(startTime, "MM-dd");
            axis.add(timeFormat);
            startTime=startTime.plusDays(1);
        }
        axis.add(DateUtil.localDateTimeFormat(startTime,"MM-dd"));
        return axis;
    }

    public abstract void setFilterTime(StatisticsRequestDto statisticsRequestDto);

    public abstract List<String> getAxis();

    public static void main(String[] args) {
        System.out.println(getBetweenMonthAxis(LocalDateTime.now().withMonth(2),LocalDateTime.now()));
//        System.out.println(getBetweenDayAxis(LocalDateTime.now().withDayOfMonth(2),LocalDateTime.now()));
    }

}
